﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.AuditLogging.LogEntry;
using System.Net;

namespace Sinacor.AuditLogging.LogEntry
{
    [Serializable]
    public class SinacorGenericLogEntry : SinacorBaseLogEntry
    {
        private const int LOG_SUBTYPE_DB_SIZE = 50;
        private const int LOG_DETAILKEY_DB_SIZE = 50;
        private const int LOG_TXT_CNTD_DET_LOG_DB_SIZE = 4000;

        #region Propriedade Preenchidas manualmente
        private List<KeyValuePair<string, string>> Details
        {
            get;
            set;
        }

        /// <summary>
        /// SubTipo do log, utilizado para categorizar os logs
        /// </summary>
        private string _logSubType;
        public string LogSubType
        {
            get
            {
                return _logSubType;
            }
            set
            {
                if (value != null && value.Length > LOG_SUBTYPE_DB_SIZE)
                    throw new ArgumentException(string.Format("LogSubType length must be less than {0}.", (1 + LOG_SUBTYPE_DB_SIZE).ToString()));

                _logSubType = value;
            }
        }
        #endregion

        public int DetailCount
        {
            get
            {
                return this.Details.Count;
            }
        }

        public SinacorGenericLogEntry(string logSubType)
            : base()
        {
            this.LogType = "SinacorGenericLog";
            this.LogSubType = logSubType;
            this.Details = new List<KeyValuePair<string, string>>();
        }

        public void AddDetail(string key, string value)
        {
            if (key.Length > LOG_DETAILKEY_DB_SIZE)
                throw new ArgumentException(string.Format("Key length must be less than {0}.", (1 + LOG_DETAILKEY_DB_SIZE).ToString()));

            if (value.Length > LOG_TXT_CNTD_DET_LOG_DB_SIZE)
                throw new ArgumentException(string.Format("Value length must be less than {0}.", (1 + LOG_DETAILKEY_DB_SIZE).ToString()));

            this.Details.Add(new KeyValuePair<string, string>(key, value));
        }

        public KeyValuePair<string, string> GetDetail(int index)
        {
            return this.Details[index];
        }

        public KeyValuePair<string, string> GetDetail(string key)
        {
            return this.Details.FirstOrDefault(d => d.Key == key);
        }
    }
}
